﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class course2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {

        }
        else
        {
            string sql = "select * from course";
            initTH();
            initGridView(sql);
        }
    }
    void initTH()
    {
        HtmlTableRow rowtemp = new HtmlTableRow();
        HtmlTableCell celltemp1 = new HtmlTableCell("th");
        celltemp1.InnerText = "课程名";
        rowtemp.Cells.Add(celltemp1);
        celltemp1 = new HtmlTableCell("th");
        celltemp1.InnerText = "作者";
        rowtemp.Cells.Add(celltemp1);
        celltemp1 = new HtmlTableCell("th");
        celltemp1.InnerText = "出版社";
        rowtemp.Cells.Add(celltemp1);
        celltemp1 = new HtmlTableCell("th");
        celltemp1.InnerText = "操作";
        rowtemp.Cells.Add(celltemp1);
        booksTable.Rows.Add(rowtemp);
    }
    void initGridView(string sql)
    {
        MySqlConnection con = DbUtils.getCon();
        MySqlCommand cmd = new MySqlCommand(sql, con);
        MySqlDataReader dr = cmd.ExecuteReader();
        
        while (dr.Read())
        {
            HtmlTableRow rowtemp = new HtmlTableRow();
            HtmlTableCell celltemp1 = new HtmlTableCell();
            celltemp1.InnerText = dr["cname"].ToString();
            rowtemp.Cells.Add(celltemp1);
            string[] author_publisher = dr["author"].ToString().Split(';');
            string author = author_publisher[0];
            string publisher = author_publisher[1];
            HtmlTableCell celltemp2 = new HtmlTableCell();
            celltemp2.InnerText =author;
            rowtemp.Cells.Add(celltemp2);
            HtmlTableCell celltemp3 = new HtmlTableCell();
            celltemp3.InnerText =publisher;
            rowtemp.Cells.Add(celltemp3);
            HtmlTableCell celltemp4 = new HtmlTableCell();
            celltemp4.InnerHtml = "<button class='layui-btn layui-btn-normal layui-btn-xs lookmore' type='button'>查看更多</button>";
            rowtemp.Cells.Add(celltemp4);
            booksTable.Rows.Add(rowtemp);
        }
        DbUtils.close(con, dr);
    }

    protected void search_btn_ServerClick(object sender, EventArgs e)
    {
        if (Request.Form.Get("search_key") == null|| Request.Form.Get("search_key").Equals(""))
        {
            //error.Style.Remove("display");
            //error.InnerText = "关键词不能为空";
        }
        else
        {
            //if(error.Style.Value.Equals("color:red;"))
            //     error.Style.Add("display","none");
            string title = Request.Form.Get("search_key").ToString();
            //error.InnerText = title + "aaaaaaa";
            booksTable.Rows.Clear();
            initTH();
            string sql = "select * from course where cname like '%" + title + "%'";
            initGridView(sql);
        }
    }

    protected void refresh_btn_Click(object sender, EventArgs e)
    {
        booksTable.Rows.Clear();
        initTH();
        string sql = "select * from course";
        initGridView(sql);
    }
}